package nil.ed.simulator.request.ui.jfx.event;

import javafx.application.Platform;
import nil.ed.simulator.request.common.QQItem;
import nil.ed.simulator.request.enums.events.EventType;
import nil.ed.simulator.request.event.Event;
import nil.ed.simulator.request.event.bus.EventListener;
import nil.ed.simulator.request.ui.jfx.bean.qq.QQRow;
import nil.ed.simulator.request.ui.jfx.controller.TableViewController;
import nil.ed.simulator.request.ui.jfx.manager.ControllerManager;

import java.util.List;

/**
 * @author lidelin.
 */
public class NeedVcEventListener implements EventListener {
    @Override
    public void onEvent(Event event) {
        QQItem qqItem = (QQItem) event.getData();
        TableViewController tableViewController = ControllerManager.getTable();
        List<Object> dataList = tableViewController.getDataList();
        for (Object obj : dataList) {
            QQRow qqRow = (QQRow) obj;
            if (qqRow.getQqItem() == qqItem) {
                Platform.runLater(() -> {
                    qqRow.setCookieStatus(QQRow.COOKIE_STATUS_NEED_VC);
                    tableViewController.getTableView().refresh();
                });
            }
        }
    }

    @Override
    public boolean isSupport(Event event) {
        return event.getEventType() == EventType.NEED_VC;
    }
}
